#include <stdint.h>

extern char _estack;
extern char  _sdata;
extern char  _edata;
extern char _sidata;
extern char _sbss;
extern char _ebss;


extern void SystemInit(void);
extern void main(void);

void Default_Handler()  			__attribute__ ((weak));
void Reset_handler(void)   		  __attribute__((weak));
void NMI_Handler(void ) 			__attribute__ ((weak,  alias("Default_Handler")));
void HardFault_Handler(void) 		__attribute__ ((weak,  alias("Default_Handler")));
void MemManage_Handler(void ) 		__attribute__ ((weak,  alias("Default_Handler")));
void BusFault_Handler(void ) 		__attribute__ ((weak,  alias("Default_Handler")));
void UsageFault_Handler(void )		__attribute__ ((weak,  alias("Default_Handler")));
void SVC_Handler(void ) 			__attribute__ ((weak,  alias("Default_Handler")));
void DebugMon_Handler(void ) 		__attribute__ ((weak,  alias("Default_Handler")));
void PendSV_Handler(void ) 			__attribute__ ((weak,  alias("Default_Handler")));
void SysTick_Handler(void ) 		__attribute__ ((weak,  alias("Default_Handler")));

void WWDG_IRQHandler(void )  	    __attribute__ ((weak,  alias("Default_Handler")));
void PVD_PVM_IRQHandler(void ) 	    __attribute__ ((weak,  alias("Default_Handler")));
void TAMP_STAMP_IRQHandler(void )   __attribute__ ((weak,  alias("Default_Handler")));
void RTC_WKUP_IRQHandler()          __attribute__ ((weak,  alias("Default_Handler")));
void FLASH_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void RCC_IRQHandler()               __attribute__ ((weak,  alias("Default_Handler")));
void EXTI0_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void EXTI1_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void EXTI2_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void EXTI3_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void EXTI4_IRQHandler()             __attribute__ ((weak,  alias("Default_Handler")));
void DMA1_Stream0_IRQHandler()      __attribute__ ((weak,  alias("Default_Handler")));
void DMA1_Stream1_IRQHandler()      __attribute__ ((weak,  alias("Default_Handler")));
void DMA1_Stream2_IRQHandler()      __attribute__ ((weak,  alias("Default_Handler")));
void DMA1_Stream3_IRQHandler()      __attribute__ ((weak,  alias("Default_Handler")));
void DMA1_Stream4_IRQHandler()      __attribute__ ((weak,  alias("Default_Handler")));


__attribute((section(".isr_vector")))
uint32_t *isr_vectors[] = {
	(uint32_t *) (&_estack) ,
	(uint32_t *) Reset_handler,
	(uint32_t *) NMI_Handler,
	(uint32_t *) HardFault_Handler,
	(uint32_t *) MemManage_Handler,
	(uint32_t *) BusFault_Handler,
	(uint32_t *) UsageFault_Handler,
	0,
	0,
	0,
	0,
	(uint32_t *) SVC_Handler,
	(uint32_t *) DebugMon_Handler,
	0,
	(uint32_t *) PendSV_Handler,
	(uint32_t *) SysTick_Handler,
	(uint32_t *) WWDG_IRQHandler,
	(uint32_t *) PVD_PVM_IRQHandler,
	(uint32_t *) TAMP_STAMP_IRQHandler,
	(uint32_t *) RTC_WKUP_IRQHandler,
	(uint32_t *) FLASH_IRQHandler,
	(uint32_t *) RCC_IRQHandler,
	(uint32_t *) EXTI0_IRQHandler,
	(uint32_t *) EXTI1_IRQHandler,
	(uint32_t *) EXTI2_IRQHandler,
};


//void init_runtime(void)
__attribute((section(".reset_fun")))
void Reset_handler(void)
{
     __asm("    ldr    sp, =_estack");

    //copy data from flash to ram
   char *flash_src =  &_sidata;
   char *ram_dest = &_sdata;

/*
   while(ram_dest < &_edata)
	*ram_dest ++ = *flash_src++;
*/
    //init bss with 0 
    for(ram_dest = &_sbss; ram_dest < &_ebss; ram_dest++ )
	*ram_dest  =  0x0;

    SystemInit();	
   // __libc_init_array();
    main();
}

 __attribute__((weak))
void Default_Handler(void){
	while(1){};
}

